package cn.Y_Qiu.pay.service.impl;

import cn.Y_Qiu.basic.exception.BusinessException;
import cn.Y_Qiu.basic.service.impl.BaseServiceImpl;
import cn.Y_Qiu.order.domain.OrderAddress;
import cn.Y_Qiu.order.mapper.OrderAddressMapper;
import cn.Y_Qiu.pay.domain.AlipayInfo;
import cn.Y_Qiu.pay.domain.PayBill;
import cn.Y_Qiu.pay.mapper.AlipayInfoMapper;
import cn.Y_Qiu.pay.mapper.PayBillMapper;
import cn.Y_Qiu.pay.service.IAlipayInfoService;
import cn.Y_Qiu.pay.service.IPayBillService;
import cn.Y_Qiu.user.domain.UserAddress;
import cn.Y_Qiu.user.mapper.UserAddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class AplipayInfoServiceImpl extends BaseServiceImpl<AlipayInfo> implements IAlipayInfoService {
    @Autowired
    private AlipayInfoMapper alipayInfoMapper;

    @Autowired
    private PayBillMapper payBillMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private OrderAddressMapper orderAddressMapper;
    @Override
    public AlipayInfo getByShopId(Long shop_id) {
        return alipayInfoMapper.loadByShopId(shop_id);
    }

    @Override
    @Transactional
    public Map<String, Object> getOrderInfo(String url) {
        if (url == null || url == ""){
            throw new BusinessException("订单参数为空");
        }
        /*
        * http://localhost/success.html?charset=utf-8
        * &out_trade_no=0717102626
        * &method=alipay.trade.page.pay.return
        * &total_amount=60.00
        * &sign=jIJRYS0M%2Bdh5r8kSyS%2BPTSCEPRlxzBVj%2B9%2Bh0rZPp8hUEih0cyb4KfjWDSpSx8cGRZMA1yIpSwkL%2BGqwNbohZOa%2F0gj3mpUy9uvWK08BppoZB7X3aFqrhuwZkDv8uP22wVl6njZnLQDkwXhVRhXwPYDLGa6VIgOshZB%2Ff4QYof2vDU7TZxrgNUUvhubKm%2B8I0vYylkdsu2GAAd5wSwz9sJroSBoQVAU%2FzkxguQ%2BEToaKGtjCioG5t1Q0JGmA2cMAzsLJek4YGuoxJ6yoL%2B8xsABGLlI9v5QFKB4svVdgxyJWzxf0TY1yNO%2BDDQGrX18veMrp3zkvkdzhBN85lV%2BbRg%3D%3D
        * &trade_no=2021071022001436260501225148
        * &auth_app_id=2021000117686248&version=1.0
        * &app_id=2021000117686248&sign_type=RSA2
        * &seller_id=2088621956115932&timestamp=2021-07-10+11%3A51%3A29
        * */
        String[] paraStr = url.split("&");
        String[] paraStrs = paraStr[1].split("=");
        String out_trade_no = paraStrs[1];
        //System.out.println(out_trade_no);
        PayBill bill = payBillMapper.loadByUnionPaySn(out_trade_no);
        String username = bill.getNickName();
        BigDecimal price = bill.getMoney();
        OrderAddress orderAddress = orderAddressMapper.queryByOrderSn(out_trade_no);
        String address = orderAddress.getAddress();
        String phone = orderAddress.getPhone();
        /*
                前端需要的值
                price:null,
                username:"没有绑定好的值",
                phone:null,
                address:null
        * */
        Map<String , Object> map = new HashMap<>();
        map.put("price", price);
        map.put("username", username);
        map.put("phone", phone);
        map.put("address", address);
        return map;
    }
}
